import QtQuick 2.0
import DataMan 0.1
import "../utils.js" as Utils;
import "../logic.js" as Logic;

Item {
    anchors.fill: parent
    property bool newgame: false

    Image {
	    id: root
	    anchors.fill: parent
	    source: "image://default/game/bg.jpg"

	    ImgBtn {
		    id: backBtn
		    source: "image://default/game/back.png"
		    y: 5 * Utils.DesiginScaleFactor.y
		    x: 10 * Utils.DesiginScaleFactor.x
		    showAni: true
		    onClicked: {
			    if (!Logic.game.canmove && !Logic.game.win)
				    toMainScene();
			    else
				    returnDialog.visible = true;
		    }
	    }
	    ImgBtn {
		    id: nextBtn
		    source: "image://default/game/next_lv.png"
		    y: 5 * Utils.DesiginScaleFactor.y
		    x: root.width - width - 30
		    showAni: true
		    onClicked: {
			    if (Logic.game.win) {
				    // 提示还能移动。不能移动的情况下将自动切关
				    nextLvDialog.visible = true;
			    }
		    }
	    }

	    Row {
		    id: propIconView
		    width: root.width * 0.9
		    height: 75 * Utils.DesiginScaleFactor.y
		    x: root.width * 0.05
		    y: targetScLbl.y + 50
		    spacing: width / PropTypeN / 4 + 2
		    Prop {
			    type: DataMan.PDSc
			    normalImg: "image://default/game/double_score.png";
		    }
		    Prop {
			    type: DataMan.PHar
			    normalImg: "image://default/game/hammer.png";
		    }
		    Prop {
			    type: DataMan.PRe
			    rAniLoopTime: 5
			    normalImg: "image://default/game/plant.png";
		    }
		    Prop {
			    type: DataMan.PZom
			    rAniLoopTime: 4
			    normalImg: "image://default/game/zoomer.png";
		    }
		    Prop {
			    type: DataMan.PHeart
			    rAniLoopTime: 3
			    normalImg: "image://default/game/red_heart.png";
		    }
	    }
	    Row {
		    id: propNumView
		    width: propIconView.width
		    spacing: 75 * Utils.DesiginScaleFactor.y - 1
		    x: 75 * Utils.DesiginScaleFactor.x - 2
			y: propIconView.y + propIconView.height - 14
			PropNum{type: DataMan.PDSc}
			PropNum{type: DataMan.PHar}
			PropNum{type: DataMan.PRe}
			PropNum{type: DataMan.PZom}
			PropNum{type: DataMan.PHeart}
	    }

	    Item {
		    id: canvas
		    width: root.width
		    height: root.sourceSize.width * Utils.DesiginScaleFactor.y
		    y: root.height - height - 16 * Utils.DesiginScaleFactor.y
		    MouseArea {
			    anchors.fill: parent
			    acceptedButtons: Qt.LeftButton
			    onClicked:  {
				    var col = Math.floor(mouseX * GameCol / width);
				    var row = Math.floor(mouseY * GameRow / height);
				    Logic.handleTouch(col, row);
			    }
		    }
	    }

	    // NOTE: debug touch area
	    //    Rectangle {
	    //	    anchors.fill: canvas
	    //	    border.width: 2
	    //	    border.color: "red"
	    //	    color: "transparent"
	    //    }

	    ImgLbl {
		    id: nowScLbl
		    x: 120 * Utils.DesiginScaleFactor.x
		    y: 103 * Utils.DesiginScaleFactor.y
		    img: "image://default/gamenum/"
	    }

	    ImgLbl {
		    id: targetScLbl
		    x: root.width - 120 * Utils.DesiginScaleFactor.x
		    y: nowScLbl.y
		    img: "image://default/gamenum/"
	    }

	    ImgLbl {
		    id: lvLbl
		    x: root.width - 46 * Utils.DesiginScaleFactor.x
		    y: 67 * Utils.DesiginScaleFactor.y
		    img: "image://default/gamenum/"
	    }

	    ImgLbl {
		    id: highScLbl
		    x: root.width / 2 - 70
		    y: 40 * Utils.DesiginScaleFactor.y
		    img: "image://default/highscnum/"

		    function setDrawStr() {
			    var s = num.toString();
			    var i = 8 - s.length;
			    if (i > 0)
				    drawStr = "oooooooo".substr(0, i) + s;
			    else
				    drawStr = s;
		    }
	    }

	    Connections {
		    target: DataMan
		    onLvChanged: lvLbl.num = DataMan.lv;
	    }

	    Connections {
		    target: DataMan
		    onNowScChanged: nowScLbl.num = DataMan.nowSc;
	    }

	    Connections {
		    target: DataMan
		    onHighScChanged: highScLbl.num = DataMan.highSc;
	    }

	    Text {
		    id: scText
		    text: "两次点击相连的小怪兽就能消灭它们"
		    font.pixelSize: 24
		    color: "white"
		    x: 32 * Utils.DesiginScaleFactor.x
		    y: 240 * Utils.DesiginScaleFactor.y
	    }

	    Image {
		    anchors.fill: parent
		    source: "image://default/game/big_mask.png"
	    }

	    Timer {
		    property var timeoutfunc: null
		    id: gtimer
		    running: false
		    triggeredOnStart: true
		    repeat: true
		    onTriggered: {
			    if (timeoutfunc)
				    timeoutfunc();
		    }
	    }

	    MouseArea {
		    id: viewMask
		    anchors.fill: root
		    enabled: true
	    }

	    Component.onCompleted: {
			var objarr = {
				view: canvas,
				scTip: scText,
				timer: gtimer,
				tipUsePropDialog: tipUsePropDialog,
				viewMask: viewMask
			};
		    Logic.createGame(newgame, objarr, function(){
			    targetScLbl.num = Logic.targetScore;
		    });
		    nowScLbl.num = DataMan.nowSc;
		    highScLbl.num = DataMan.highSc;
		    lvLbl.num = DataMan.lv;
	    }
    }


    function toMainScene()
    {
	    Logic.endGame();
	    Utils.AppWin.runScene("MainScene{}");
    }

    AskDialog {
	    id: returnDialog
	    visible: false
	    msg: qsTr("游戏还在进行，确定返回<br>主菜单吗？")
	    onOk: {
		    toMainScene();
	    }
    }

    AskDialog {
	    id: nextLvDialog
	    visible: false
	    msg: qsTr("还有可以消除的小怪。留<br>下消完可获得更多分数。<br>仍旧进入下一关吗？")
	    onOk: {
		    Logic.enterNextLv();
	    }
    }

    AskDialog {
	    id: tipUsePropDialog
	    tipMode: true
	    visible: false
	    msg: qsTr("通关失败，快点使用道具吧!")
    }
}

